package comframe.jdbc.sql.support.struct;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;

import comframe.jdbc.annotation.Generator;
import comframe.jdbc.annotation.Id;
import comframe.jdbc.annotation.Table;
import comframe.jdbc.sql.convert.ConvertUtil;

/**
 * 解析javaBean
 * 
 * @author ray
 * 
 */
public class BeanParse {

	protected String getFiled(Class<?> clazz) {
		// 保存数据库字段
		StringBuffer fileds = new StringBuffer();
		// 判断是否是table 注解了
		if (clazz.isAnnotationPresent(Table.class)) {
			getStringFiled(clazz,fileds);
		}
		// 拼接字符串
		if ("".equals(fileds.toString())) {
			fileds.append(",").append(" * ");
		}

		return fileds.toString().substring(1);
	}

	private void getStringFiled(Class<?> clazz, StringBuffer fileds) {
		for (Field field : clazz.getDeclaredFields()) {
			// 字段名称
			String filedName = "";
			// 获得字段 注解对象
			Annotation[] ans = field.getAnnotations();
			// 遍历注解对象
			for (Annotation an : ans) {
				if (comframe.jdbc.annotation.Field.class == an.annotationType()) {
					filedName = ((comframe.jdbc.annotation.Field) an).name();
				} else if (Id.class == an.annotationType()) {
					filedName = ((Id) an).name();
				}

				if (filedName != null && "".equals(filedName)) {
					filedName = field.getName();
				}
			}
			// 拼接字符串
			if (filedName != null && !"".equals(filedName)) {
				fileds.append(",").append(filedName);
			}
		}
		if(clazz.getSuperclass() != null){
			getStringFiled(clazz.getSuperclass(),fileds);
		}
	}

	/**
	 * 获取表名称
	 * 
	 * @param clazz
	 * @return
	 * @throws SecurityException
	 * @throws NoSuchFieldException
	 */
	protected String getTable(Class<?> clazz) {
		String tableName = "";
		if (clazz.isAnnotationPresent(Table.class)) {
			Table table = clazz.getAnnotation(Table.class);
			tableName = table.name();

			if (tableName != null && "".equals(tableName)) {
				String temp = clazz.getName();
				tableName = temp.substring(temp.lastIndexOf(".") + 1)
						.toLowerCase();
			}
		}

		return tableName;
	}
	
	

	/**
	 * 获取表名称
	 * 
	 * @param clazz
	 * @return
	 * @throws SecurityException
	 * @throws NoSuchFieldException
	 */
	protected String getKey(Class<?> clazz) {
		String key = null;
		if(clazz.getSuperclass() != null){
			key = getKey(clazz.getSuperclass());
		}
		if (clazz.isAnnotationPresent(Table.class)) {
			for (Field field : clazz.getDeclaredFields()) {
				// 获得字段 注解对象
				Annotation[] ans = field.getAnnotations();
				// 遍历注解对象
				for (Annotation an : ans) {
					if (Id.class == an.annotationType()) {
						key = ((Id) an).name();
						if (key != null) {
							key = field.getName();
							break;
						}
					}

				}
			}
		}
		
		return key;
	}
	/**
	 * 生成更新语句  
	 * @param object
	 * @return
	 */
	protected String getInsert(Object object){
		StringBuffer  insertSql = new StringBuffer();
		StringBuffer  fields = new StringBuffer();
		StringBuffer  values = new StringBuffer();
		Class<? extends Object> clazz = object.getClass();
		// 判断是否是table 注解了
		if (clazz.isAnnotationPresent(Table.class)) {
			 getInsertString(object ,clazz,fields,values);
		} 
		insertSql.append("(").append(fields).append(") values (").append(values).append(")");
		return insertSql.toString();
	}
  
	private void getInsertString(Object object, Class<? extends Object> clazz,
			StringBuffer fields, StringBuffer values) {
		for (Field field : clazz.getDeclaredFields()) {
			//判断值不为空的时候拼接
			// 字段名称
			String filedName = "";
			// 名称
			String SeqName = "";
		
			// 获得字段 注解对象
			Annotation[] ans = field.getAnnotations();
			// 遍历注解对象
			
			for (Annotation an : ans) {
				if (comframe.jdbc.annotation.Field.class == an.annotationType()) {
					filedName = ((comframe.jdbc.annotation.Field) an).name();
				} else if (Id.class == an.annotationType()) {
					filedName = ((Id) an).name();
				
				} else if(Generator.class == an.annotationType()){
					SeqName = ((Generator) an).name();	
				} 

				if (filedName != null && "".equals(filedName)) {
					filedName = field.getName();
				}
				
			}
			//拼接sql
			if(!"".equals(filedName)){
				if(fields.length()>0){
					fields.append(",");
				}
					
				fields.append(filedName);
				
				if(values.length()>0){
					values.append(",");
				}
				if(!"".equals(SeqName)){
					values.append(SeqName+".nextval");
					SeqName = "";
				}else{
					values.append("?");
				}
				
			}
		}
		
		if(clazz.getSuperclass() != null){
		 getInsertString(object, clazz.getSuperclass(), fields, values);
		}
	}

	/**
	 * 生成更新语句  
	 * @param object
	 * @return
	 */
	protected String getUpdate(Object object){
		StringBuffer  fields = new StringBuffer();
		Class<? extends Object> clazz = object.getClass();
		// 判断是否是table 注解了
		if (clazz.isAnnotationPresent(Table.class)) {
			   getUpdateString(object,clazz,fields);
		}
		return fields.toString();
	}
	
	private void getUpdateString(Object object, Class<? extends Object> clazz,
			StringBuffer fields) {
		for (Field field : clazz.getDeclaredFields()) {
			// 字段名称
			String filedName = "";
			// 获得字段 注解对象
			Annotation[] ans = field.getAnnotations();
			// 遍历注解对象
			
			for (Annotation an : ans) {
				if (comframe.jdbc.annotation.Field.class == an.annotationType()) {
					filedName = ((comframe.jdbc.annotation.Field) an).name();
				} else if (Id.class == an.annotationType()) {
					filedName = ((Id) an).name();
				
				} 

				if (filedName != null && "".equals(filedName)) {
					filedName = field.getName();
				}
				
			}
			//拼接sql
			if(!"".equals(filedName)){
				if(fields.length()>0){
					fields.append(" , ");
				}
				fields.append(filedName).append(" = ? ");	
			}
		}
		
		if(clazz.getSuperclass() != null){
			getUpdateString(object, clazz.getSuperclass(), fields);
		}
	}

	/**
	 * 生成更新语句  
	 * @param object
	 * @return
	 */
	protected String getUpdateIsExist(Object object){
		StringBuffer  fields = new StringBuffer();
		
		Class<? extends Object> clazz = object.getClass();
		// 判断是否是table 注解了
		if (clazz.isAnnotationPresent(Table.class)) {
			getUpdateisExistString(object,clazz,fields);
		}
				
		return fields.toString();
	}

	private void getUpdateisExistString(Object object,
			Class<? extends Object> clazz, StringBuffer fields) {
		for (Field field : clazz.getDeclaredFields()) {
			// 字段名称
			String filedName = "";
			
			// 获得字段 注解对象
			Annotation[] ans = field.getAnnotations();
			// 遍历注解对象
			
			for (Annotation an : ans) {
				if (comframe.jdbc.annotation.Field.class == an.annotationType()) {
					filedName = ((comframe.jdbc.annotation.Field) an).name();
				} else if (Id.class == an.annotationType()) {
					filedName = ((Id) an).name();
				
				} 

				if (filedName != null && "".equals(filedName)) {
					filedName = field.getName();
				}
				
			}
			//拼接sql
			if(!"".equals(filedName)){
				Object obj = ConvertUtil.getProperty(object, filedName);
				if(fields.length() > 0 && obj != null){
					fields.append(" , ");
				}
				
				if(obj != null)
				  fields.append(filedName).append(" = ? ");	
			}
		}
		
		if(clazz.getSuperclass() != null){
			getUpdateisExistString(object, clazz.getSuperclass(), fields);
		}
	}

}
